/**
 * 订单管理
 */
var pageCurr;
var form;
$(function() {
    layui.use('table', function(){
        var table = layui.table;
        form = layui.form;

        tableIns=table.render({
            elem: '#orderList',
            url:'/order/getOrderList',
            method: 'post', //默认：get请求
            cellMinWidth: 80,
            page: true,
            request: {
                pageName: 'pageNum', //页码的参数名称，默认：pageNum
                limitName: 'pageSize' //每页数据量的参数名，默认：pageSize
            },
            response:{
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 200, //成功的状态码，默认：0
                countName: 'totals', //数据总数的字段名称，默认：count
                dataName: 'list' //数据列表的字段名称，默认：data
            },
            cols: [[
                {type:'numbers'}
                ,{field:'orderId', title:'订单编号',align:'center'}
                //,{field:'detailId', title:'订单详情',align:'center'}
               // ,{field:'category2Id', title:'服务ID',align:'center'}
                ,{field:'category2Name', title: '服务名称',align:'center'}
                ,{field:'serviceTime', title: '服务时间',align:'center'}
                ,{field:'orderAmount', title: '订单总额',align:'center'}
                ,{field:'orderStatus', title: '订单状态',align:'center'}

               // ,{field:'orderRemark', title:'订单留言',align:'center'}
               // ,{field:'buyerOpenid', title:'用户ID',align:'center'}
               // ,{field:'addressId', title:'地址ID',align:'center'}
                ,{field:'userNickname', title: '下单人',align:'center'}
               // ,{field:'addressUserName', title: '联系人姓名',align:'center'}
               // ,{field:'addressBuyerPhone', title: '联系电话',align:'center'}
               // ,{field:'addressArea', title: '地区',align:'center'}

               // ,{field:'addressDetail', title:'详细地址',align:'center'}
               // ,{field:'workerOpenid', title:'接单人ID',align:'center'}

                ,{field:'workerName', title: '接单人',align:'center'}
                //,{field:'workerPhone', title:'接单人电话',align:'center'}
                //,{field:'workerRemark', title: '取消留言',align:'center'}
                ,{field:'createTime1', title: '创建时间',align:'center'}
                ,{field:'updateTime1', title: '更新时间',align:'center'}


                ,{title:'操作',align:'center', toolbar:'#optBar'}
            ]],
            done: function(res, curr, count){
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                //console.log(res);
                //得到当前页码
                console.log(curr);
                $("[data-field='orderStatus']").children().each(function(){
                    if($(this).text()=='0'){
                        $(this).text("已下单")
                    }else if($(this).text()=='1'){
                        $(this).text("已接单")
                    }else if($(this).text()=='2'){
                        $(this).text("已完成")
                    }else if($(this).text()=='3'){
                        $(this).text("已取消")
                    }
                });
                //得到数据总量
                //console.log(count);
                pageCurr=curr;
            }
        });

        //监听工具条
        table.on('tool(orderTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'see'){
                //查看
                seeOrder(data,"查看详情");
            } else if(obj.event === 'edit'){
                //编辑
                openOrder(data,"留言");
            }
        });

        //监听提交
        form.on('submit(orderSubmit)', function(data){
            // TODO 校验
            formSubmit(data);
            return false;
        });
    });

    //搜索框
    layui.use(['form','laydate'], function(){
        var form = layui.form ,layer = layui.layer
            ,laydate = layui.laydate;
        ///////
       /*var src='<option value="">请选择...</option>';
        $.ajax({
            url:'/order/getOrderStatus',
            dataType:'json',
            async: true,
            success:function(data){
                $.each(data,function(index,item){
                    if(!orderStatus){
                        var option = new Option(item.name,item.orderStatus);
                    }else {
                        var option = new Option(item.name,item.orderStatus);
                        // // 如果是之前的parentId则设置选中
                        if(item.orderStatus == orderStatus) {
                            option.setAttribute("selected",'true');
                        }
                    }
                    $('#orderStatus').append(option);//往下拉菜单里添加元素
                    form.render('select'); //这个很重要
                })
            }
        });*/

        ////////
        //TODO 数据校验
        //监听搜索框
        form.on('submit(searchSubmit)', function(data){
            //重新加载table
            load(data);
            return false;
        });
    });
});

//提交表单
function formSubmit(obj){
    $.ajax({
        type: "POST",
        data: $("#orderForm").serialize(),
        url: "/order/setOrder",
        success: function (data) {
            if (data.code == 1) {
                layer.alert(data.msg,function(){
                    layer.closeAll();
                    load(obj);
                });
            } else {
                layer.alert(data.msg);
            }
        },
        error: function () {
            layer.alert("操作请求错误，请您稍后再试",function(){
                layer.closeAll();
                //加载load方法
                load(obj);//自定义
            });
        }
    });
}


function openOrder(data,title){

    if(data==null || data==""){
        $("#orderId0").val("");
    }else{
        $("#orderId0").val(data.orderId);
        $("#workerRemark").val(data.workerRemark);

    }
    var pageNum = $(".layui-laypage-skip").find("input").val();
    $("#pageNum").val(pageNum);

    layer.open({
        type:1,
        title: title,
        fixed:false,
        resize :false,
        shadeClose: true,
        area: ['550px'],
        content:$('#setOrder'),
        end:function(){
            cleanOrder();
        }
    });
}



function seeOrder(data,title) {
    if(data==null || data==""){
        $("#orderId1").val("");
    }else{
        $("#orderId1").val(data.orderId);
        $("#category2Name").val(data.category2Name);
        $("#serviceTime").val(data.serviceTime);
        $("#buyerOpenid").val(data.buyerOpenid);
        $("#userNickname").val(data.userNickname);
        $("#addressUserName").val(data.addressUserName);
        $("#addressBuyerPhone").val(data.addressBuyerPhone);
        $("#addressArea").val(data.addressArea);
        $("#addressDetail").val(data.addressDetail);
        $("#orderRemark").val(data.orderRemark);
        $("#workerOpenid").val(data.workerOpenid);
        $("#workerName").val(data.workerName);
        $("#workerPhone").val(data.workerPhone);
        $("#workerRemark0").val(data.workerRemark);
        $("#createTime1").val(data.createTime1);
        $("#updateTime1").val(data.updateTime1);


    }
    var pageNum = $(".layui-laypage-skip").find("input").val();
    $("#pageNum").val(pageNum);

    layer.open({
        type:1,
        title: title,
        fixed:false,
        resize :false,
        shadeClose: true,
        area: ['1200px', '650px'],
        content:$('#getOrder'),
        end:function(){
            cleanOrder();
        }
    });
}


function load(obj){
    //重新加载table
    tableIns.reload({
        where: obj.field
        , page: {
            curr: pageCurr //从当前页码开始
        }
    });
}

function cleanOrder(){
    $("#workerRemark").val("");


}
